﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 音频资源规则配置脚本
    /// </summary>
    [Serializable]
    public sealed class AudioImportConfig
    {
        [SerializeField]
        private AudioClipLoadTypeVariate audioLoadType;

        [Tooltip("采样率设置")]
        [SerializeField]
        private AudioSampleRateSettingVariate sampleRateSetting;
        [SerializeField]
        private AudioRuleSampleRateVariate sampleRateOverride;

        [Tooltip("声音在运行时将会被使用的指定的格式")]
        [SerializeField]
        private AudioCompressionFormatVariate compressionFormat;

        [Tooltip("压缩品质(0-1)")]
        [SerializeField]
        private FloatVariate quality;
        //[SerializeField]
        //private IntVariate conversionMode;

        [Tooltip("多声道音频将在打包前混合为单声道")]
        [SerializeField]
        private BoolVariate forceToMono;

        [Tooltip("加载将在单独的线程上延时进行，不会阻止主线程")]
        [SerializeField]
        private BoolVariate loadInBackground;

        [Tooltip("立体混响声")]
        [SerializeField]
        private BoolVariate ambisonic;

        [Tooltip("预加载，标志此资源需要在场景加载时提前加载到内存中。一般短音频开启，背景音乐不要开启，会造成游戏场景加载卡顿。")]
        [SerializeField]
        private BoolVariate preloadAudioData;

        public void MatchRule(AudioImporter importer)
        {
           // importer.ClearSampleSettingOverride("Android");
            var settings = importer.defaultSampleSettings;
            if (this.audioLoadType.IsHasValue())
            {
                settings.loadType = this.audioLoadType.GetValue();
            }

            if (this.sampleRateSetting.IsHasValue())
            {
                settings.sampleRateSetting = this.sampleRateSetting.GetValue();
            }

            if (this.sampleRateOverride.IsHasValue())
            {
                settings.sampleRateOverride = (uint)this.sampleRateOverride.GetValue();
            }
            if (this.compressionFormat.IsHasValue())
            {
                settings.compressionFormat = this.compressionFormat.GetValue();
            }
            if (this.quality.IsHasValue())
            {
                settings.quality = this.quality.GetValue();
            }
            //if (this.conversionMode.IsHasValue())
            //{
            //    settings.conversionMode = this.conversionMode.GetValue();
            //}
            importer.defaultSampleSettings = settings;
            if (this.forceToMono.IsHasValue())
            {
                importer.forceToMono = this.forceToMono.GetValue();
            }

            if (this.loadInBackground.IsHasValue())
            {
                importer.loadInBackground = this.loadInBackground.GetValue();
            }

#if UNITY_2019
            if (this.preloadAudioData.IsHasValue())
            {
                importer.preloadAudioData = this.preloadAudioData.GetValue();
            }
#endif
            if (this.ambisonic.IsHasValue())
            {
                importer.ambisonic = this.ambisonic.GetValue();
            }

#if UNITY_2019_1_OR_NEWER
            importer.SaveAndReimport(); //unity2019版本需要主动刷新
#endif

        }

    }
}

